package com.example.administrator.readbook.ui.fragment;

import android.support.v4.app.Fragment;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ListView;

import com.example.administrator.readbook.R;
import com.example.administrator.readbook.adapter.AdapterMainListview;
import com.example.administrator.readbook.base.BaseFragment;
import com.example.administrator.readbook.greendao.BookDao;
import com.example.administrator.readbook.greendao.GreenDaoBookBean;
import com.example.administrator.readbook.navigation.MainNavigation;
import com.example.administrator.readbook.ui.BookSourceActivity;
import com.example.administrator.readbook.widget.slidingbutton.IonSlidingViewClickListener;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ShaoGeng on 2018/7/12.
 * email 1002919029@qq.com
 * GitHub https://github.com/shaogeng1994
 */

public class BookRackFragment extends BaseFragment {

    private WeakReference navigation = new WeakReference<MainNavigation>(null);
    private SwipeRefreshLayout swipeRefreshLayout;
    private ListView listView;
    private AdapterMainListview adapter;
    private List<GreenDaoBookBean> bookBeanList = new ArrayList<>();

    public static Fragment getInstance(MainNavigation navigation) {
        BookRackFragment fragment = new BookRackFragment();
        fragment.navigation = new WeakReference<MainNavigation>(navigation);
        return fragment;
    }

    @Override
    public View initLayout() {
        View view = View.inflate(getActivity(), R.layout.fragment_bookrack, null);
        view.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        return view;
    }

    @Override
    public void initViewModel() {

    }

    @Override
    public void initView() {
        listView = mContentView.findViewById(R.id.main_listview);
        swipeRefreshLayout = mContentView.findViewById(R.id.swiperefreshlayout);
        bookBeanList.addAll(BookDao.query());
        adapter = new AdapterMainListview(mContentView.getContext(), bookBeanList);
        listView.setAdapter(adapter);
        adapter.setIonSlidingViewClickListener(new IonSlidingViewClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(getActivity(), BookSourceActivity.class);
                intent.putExtra("bookId", bookBeanList.get(position).getBookId());
                intent.putExtra("bookName", bookBeanList.get(position).getBookName());
                startActivity(intent);
            }

            @Override
            public void onDeleteBtnCilck(View view, int position) {
                BookDao.deleteBook(bookBeanList.get(position).getBookId());
                bookBeanList.clear();
                bookBeanList.addAll(BookDao.query());
                adapter.notifyDataSetChanged();
            }
        });
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                bookBeanList.clear();
                bookBeanList.addAll(BookDao.query());
                adapter.notifyDataSetChanged();
                swipeRefreshLayout.setRefreshing(false);
            }
        });
    }

    @Override
    public void initListener() {

    }
}
